package com.example.xiaomengai.tools;


import com.example.xiaomengai.entity.Organization;
import com.example.xiaomengai.entity.RelationDTO;
import com.example.xiaomengai.entity.StrangeFire;
import com.example.xiaomengai.service.CharacterService;
import com.example.xiaomengai.service.impl.Neo4jRepositoryImpl;
import dev.langchain4j.agent.tool.P;
import dev.langchain4j.agent.tool.Tool;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.example.xiaomengai.entity.Character;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 功能
 * 作者：梦天
 * 日期：2025/4/26 15:40
 */
@Component
@Slf4j
public class AgentTools {
    @Autowired
    private Neo4jRepositoryImpl neo4jRepository;

    @Tool(name = "getPersonInfo", value = "根据人物姓名查询，返回人物信息")
    @Schema(description = "查询人物信息")
    public String getPersonInfo(@P(value = "姓名") String name) {
        Optional<Character> result = neo4jRepository.findByName(name);
        if (result.isEmpty()) {
            return "没有找到名为「" + name + "」的人物信息";
        }
        return result.get().toString();
    }

    @Tool(name = "getOrganizationInfo", value = "根据势力/地区名称查询，返回势力信息")
    @Schema(description = "查询势力信息")
    public String getOrganizationInfo(@P(value = "势力名称") String name) {
        Optional<Organization> result = neo4jRepository.findOrganizationByName(name);
        if (result.isEmpty()) {
            return "没有找到名为「" + name + "」的势力信息";
        }
        return result.get().toString();
    }

    @Tool(name = "getStrangeFireInfo", value = "根据异火名称查询，返回异火信息")
    @Schema(description = "查询异火信息")
    public String getStrangeFireInfo(@P(value = "异火名称") String name) {
        Optional<StrangeFire> result = neo4jRepository.findStrangeFireByName(name);
        if (result.isEmpty()) {
            return "没有找到名为「" + name + "」的异火信息";
        }
        return result.get().toString();
    }

    @Tool(name = "getRelationsBetweenCharacters", value = "查询两个人物之间的关系")
    @Schema(description = "查询人物关系")
    public String getRelationsBetweenCharacters(
            @P(value = "人物1姓名") String char1Name,
            @P(value = "人物2姓名") String char2Name) {
        List<RelationDTO> relations = neo4jRepository.findRelationsBetweenCharacters(char1Name, char2Name);
        if (relations.isEmpty()) {
            return "没有找到「" + char1Name + "」和「" + char2Name + "」之间的关系";
        }
        return relations.stream()
                .map(r -> String.format("%s → [%s] → %s", r.getC1(), r.getRelationType(), r.getC2()))
                .collect(Collectors.joining("\n"));
    }

    @Tool(name = "getCharacterRelationsByType", value = "根据关系类型查询人物的特定关系")
    @Schema(description = "查询特定类型的人物关系")
    public String getCharacterRelationsByType(
            @P(value = "人物姓名") String charName,
            @P(value = "关系类型") String relationType) {
        List<RelationDTO> relations = neo4jRepository.findCharacterRelationsByType(charName, relationType);
        if (relations.isEmpty()) {
            return "没有找到「" + charName + "」的「" + relationType + "」类型的关系";
        }
        return relations.stream()
                .map(r -> String.format("%s → [%s] → %s", r.getC1(), r.getRelationType(), r.getC2()))
                .collect(Collectors.joining("\n"));
    }
}